//
//  BYJLandingViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/28.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJLandingViewController.h"

@interface BYJLandingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *landingTableView;

@end

@implementation BYJLandingViewController

- (void)viewWillDisappear:(BOOL)animated {
    [PlayViewController sharePlayViewBar].hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createControls];
    [self createTableView];
}

- (void)returnItemAction:(UIBarButtonItem *)item {
    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]
                                                 animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}

- (void)createControls {
    UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"item"] style:UIBarButtonItemStyleDone target:self action:@selector(returnItemAction:)];
    self.navigationItem.leftBarButtonItem = returnItem;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 64)];
    imgView.backgroundColor = [UIColor cyanColor];
    imgView.image = [UIImage imageNamed:@"beijing.jpg"];
    [self.view addSubview:imgView];
}

- (void)createTableView {
    
    self.landingTableView = [[UITableView alloc] initWithFrame:CGRectMake( 0, 0, SCREEN_WIDTH, 320) style:UITableViewStylePlain];
    _landingTableView.backgroundColor = [UIColor clearColor];
    _landingTableView.delegate = self;
    _landingTableView.dataSource = self;
    _landingTableView.bounces = NO;
    _landingTableView.separatorStyle = UITableViewCellStyleDefault;
    [self.view addSubview:_landingTableView];
    
    [_landingTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    NSArray *titles = @[@"手机号/邮箱登陆", @"QQ登陆", @"新浪微博登陆", @"微信登陆"];
    NSArray *images = @[@"user_locallogin.png", @"user_thirdlogin_qq.png", @"user_thirdlogin_sina.png", @"user_thirdlogin_wechat.png"];
    cell.textLabel.text = titles[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        BYJIphoneViewController *VC = [[BYJIphoneViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if (indexPath.row == 1) {
        
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            

            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
                
                // 返回我的页面
                RootViewController *VC = [[RootViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
                
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                
                NSArray *arr = [NSArray arrayWithObjects:snsAccount.userName, snsAccount.iconURL, nil];
                [userDefaults setObject:arr forKey:@"microblog"];
//                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
            }});
    }
    
    if (indexPath.row == 2) {
        
        
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            if (response.responseCode == UMSResponseCodeSuccess) {
               
                //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
                [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                    
                   UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                    
//                    NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[snsAccount.userName, snsAccount.iconURL] forKeys:@[@"name", @"icon"]];
                    
                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    
                    NSArray *arr = [NSArray arrayWithObjects:snsAccount.userName, snsAccount.iconURL, nil];
                    [userDefaults setObject:arr forKey:@"microblog"];
                    
                    // 返回我的页面
                        RootViewController *VC = [[RootViewController alloc] init];
                        [self.navigationController pushViewController:VC animated:YES];
                    
//                    [[NSNotificationCenter defaultCenter] postNotificationName:@"Landing" object:dic userInfo:nil];
                }];
            }});
    }
    
    if (indexPath.row == 3) {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
            }
            
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
